/**
 * 
 */
package helper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * 复制表结构
 * @author alex.lee at 2010-9-15 上午11:03:16
 *
 * desc：
 */
public class SqlCreater {

	private String driverClassName;

	private String url;

	private String password;

	private String username;

	public SqlCreater(String driver,String url, String username, String passwd) {
		this.driverClassName=driver;
		this.url=url;
		this.username=username;
		this.password=passwd;
	}
	
	/**
	 * 复制表
	 * @param tableName 表名
	 * @param n 复制的数量
	 * @throws Exception
	 */
	public void execute(String tableName, int n) throws Exception{
		System.out.println("copy table start...");
		Statement st = null;
		Connection c = null;
		Class.forName(driverClassName);
		c = DriverManager.getConnection(url, username, password);
		st = c.createStatement();
		for (int i = 0; i < n; i++) {
			System.out.println("copy table :"+(i+1));
			String sql="create table "+tableName+"_"+i+" select * from "+tableName+" where 1=2";
			st.executeUpdate(sql);
		}
		st.close();
		c.close();
		System.out.println("copy table end.");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SqlCreater sc=new SqlCreater("com.mysql.jdbc.Driver",
				"jdbc:mysql://127.0.0.1:3306/frame_work", "root", "root");
		try {
			sc.execute("student",5);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
